package com.coincalf.web;

import com.coincalf.entity.User;
import com.coincalf.framework.dto.ApplyWithdrawDTO;
import com.coincalf.framework.http.Response;
import com.coincalf.framework.utils.AddressUtil;
import com.coincalf.service.AccountService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

/**
 * @Description: 数字货币提现 Controller
 * @Author: Chen Long
 * @Date: Created in 2018/5/22 下午5:21
 * @Modified by: Chen Long
 */
@RestController
@RequestMapping("/withdraw")
@Slf4j
@Api(value = "资产管理-数字货币提现", description = "资产管理-数字货币提现")
public class CoinWithdrawController {

    @Autowired
    private AccountService accountService;
    @Autowired
    private CommonController commonController;

    /**
     * 申请提币
     *
     * @return
     */
    @PostMapping
    @ApiOperation(value = "COIN-WITHDRAW-001 申请提币", notes = "申请提币", httpMethod = "POST")
    @ApiImplicitParam(name = "applyWithdraw", value = "申请提币参数", dataType = "ApplyWithdrawDTO", paramType = "body")
    //@PreAuthorize("isAuthenticated()")
    public Object applyWithdraw(
            HttpServletRequest request,
            @RequestBody ApplyWithdrawDTO applyWithdraw
    ) {
        User user = commonController.getUser(request);
        boolean b = AddressUtil.verifyAddress(applyWithdraw.getCoinId(), applyWithdraw.getAddress());
        if (!b) {
            return Response.err(-99, "地址格式不正确");
        }
        return accountService.applyWithdraw(applyWithdraw, user);
    }

}
